package com.example.shop.shops.controller;


import com.example.shop.ov.AuditShopDto;
import com.example.shop.ov.ShopOv;
import com.example.shop.shops.domain.Shop;
import com.example.shop.shops.service.ShopService;
import com.example.shop.util.AjaxResult;
import com.example.shop.util.BasePage;
import com.example.shop.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@CrossOrigin(origins = "*", maxAge = 3600)
@RequestMapping("/shop")
public class ShopController {
    @Autowired
    private ShopService shopService;

    @PostMapping("/settlement")
    public AjaxResult getShop(@RequestBody @Valid ShopOv shopOv) {
        shopService.add(shopOv);
        return AjaxResult.me().success();
    }

    @PostMapping("/list")
    public PageList<Shop> pageList(@RequestBody BasePage basePage) {
        return shopService.getPageList(basePage);
    }

    @PostMapping("/audit/pass")
    public AjaxResult auditPass(@RequestBody @Valid AuditShopDto dto) {
        shopService.auditPass(dto);
        return AjaxResult.me().success();
    }

    @GetMapping("/active/{id}")
    public AjaxResult active(@PathVariable("id") Long id) {
        shopService.active(id);
        return AjaxResult.me().success();
    }

    @PostMapping("/audit/reject")
    public AjaxResult auditReject(@RequestBody @Valid AuditShopDto dto) {
        shopService.auditReject(dto);
        return AjaxResult.me().success();
    }

    @GetMapping("/{id}")
    public Shop getById(@PathVariable("id") Long id) {
        Shop byId = shopService.getById(id);
        assert byId != null;
        return byId;
    }

}
